/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImportManager.java
 *
 * Created on 02.04.2012, 01:08:57
 */
package Editoren;

import Schnittstellen.RTSObject;
import Schnittstellen.Audio.Sound;
import Schnittstellen.Audio.Soundset;
import Schnittstellen.Audio.AudioFile;
import Schnittstellen.ObjectManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Geez
 */
public class SoundEditor extends javax.swing.JPanel {

    private ObjectManager om;
    private JFrame chooserFrame;
    private JFileChooser chooser;

    /** Creates new form ImportManager */
    public SoundEditor() {
        om = ObjectManager.getInstance();
        setupchooser();
        initComponents();
    }

    /**
     * Filechooser initialisierung
     */
    private void setupchooser() {
        chooserFrame = new JFrame("laden..");
        chooser = new JFileChooser();
        chooserFrame.add(chooser);
        chooserFrame.pack();
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        chooser.setFileFilter(new FileNameExtensionFilter("Audiofiles", "wav", "aiff"));
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().matches(JFileChooser.APPROVE_SELECTION)) {
                    addSound(chooser.getSelectedFile());
                    chooser.setSelectedFile(null);
                }
                chooserFrame.setVisible(false);
            }
        });
    }

    /**
     * Ordnet dem ObjectManager an, eine neue Sounddatei in die Assets zu laden und aktuallisiert die soundliste
     * @param f der Pfad der datei
     */
    public void addSound(File f) {
        om.addSound(new AudioFile(f.getName()));
        om.addToAssets(ObjectManager.Dateityp.SOUND, f);
        Lsounds.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        Lsounds = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        CBsoundsets = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        remove = new javax.swing.JButton();
        play = new javax.swing.JButton();
        Bimportsound = new javax.swing.JButton();
        Rwhat = new javax.swing.JRadioButton();
        Rattack = new javax.swing.JRadioButton();
        Rbirth = new javax.swing.JRadioButton();
        Rconfirm = new javax.swing.JRadioButton();
        Rdeath = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        Lsoundsetlist = new javax.swing.JList();
        add1 = new javax.swing.JButton();
        Bcreatesoundset = new javax.swing.JButton();
        Rpissed = new javax.swing.JRadioButton();
        Bremovesound = new javax.swing.JButton();
        Bremovesoundset = new javax.swing.JButton();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Lsounds.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return om.getSoundbank().size();}
            public Object getElementAt(int i) { return om.getSoundbank().get(i);}
        });
        jScrollPane1.setViewportView(Lsounds);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 110, 190));

        jLabel1.setText("SoundBank:");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 100, -1));

        CBsoundsets.setModel(new javax.swing.DefaultComboBoxModel(om.getSoundsetbank()));
        add(CBsoundsets, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 20, 90, -1));

        jLabel2.setText("Soundsets:");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 20, 90, -1));

        remove.setText("<");
        add(remove, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 200, 40, -1));

        play.setText("play");
        play.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playActionPerformed(evt);
            }
        });
        add(play, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 300, -1, -1));

        Bimportsound.setText("+");
        Bimportsound.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BimportsoundActionPerformed(evt);
            }
        });
        add(Bimportsound, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 60, -1, -1));

        buttonGroup1.add(Rwhat);
        Rwhat.setText("What");
        Rwhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RwhatActionPerformed(evt);
            }
        });
        add(Rwhat, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 260, -1, -1));

        buttonGroup1.add(Rattack);
        Rattack.setText("Attack");
        add(Rattack, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 110, -1, -1));

        buttonGroup1.add(Rbirth);
        Rbirth.setText("Birth");
        add(Rbirth, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 140, -1, -1));

        buttonGroup1.add(Rconfirm);
        Rconfirm.setText("Confirm");
        add(Rconfirm, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 170, -1, -1));

        buttonGroup1.add(Rdeath);
        Rdeath.setText("Death");
        add(Rdeath, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 200, -1, -1));

        Lsoundsetlist.setModel(new javax.swing.AbstractListModel() {

            public int getSize() {
                Soundset s = ((Soundset) CBsoundsets.getSelectedItem());
                if (s == null) {
                    return 0;
                }
                return s.getSize();
            }

            public Object getElementAt(int i) {
                Soundset s = ((Soundset) CBsoundsets.getSelectedItem());
                return s.get(i);
            }
        });
        Lsoundsetlist.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                LsoundsetlistValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(Lsoundsetlist);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 100, 110, 190));

        add1.setText(">");
        add1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                add1ActionPerformed(evt);
            }
        });
        add(add1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 140, 40, -1));

        Bcreatesoundset.setText("+");
        Bcreatesoundset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BcreatesoundsetActionPerformed(evt);
            }
        });
        add(Bcreatesoundset, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 60, -1, -1));

        buttonGroup1.add(Rpissed);
        Rpissed.setText("Pissed");
        Rpissed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RpissedActionPerformed(evt);
            }
        });
        add(Rpissed, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 230, -1, -1));

        Bremovesound.setText("-");
        Bremovesound.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BremovesoundActionPerformed(evt);
            }
        });
        add(Bremovesound, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));

        Bremovesoundset.setText("-");
        Bremovesoundset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BremovesoundsetActionPerformed(evt);
            }
        });
        add(Bremovesoundset, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 60, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void BimportsoundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BimportsoundActionPerformed
        Soundset s = new Soundset(JOptionPane.showInputDialog("Name"));
        if (s.getEditorName() != null) {
            om.addSoundset(s);
            CBsoundsets.updateUI();
        }
    }//GEN-LAST:event_BimportsoundActionPerformed

    private void BcreatesoundsetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BcreatesoundsetActionPerformed
        chooserFrame.setVisible(true);
    }//GEN-LAST:event_BcreatesoundsetActionPerformed

    private void add1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add1ActionPerformed
        if (Lsounds.getSelectedValue() != null) {
            if (CBsoundsets.getSelectedItem() != null) {
                if (Rattack.isSelected()) {
                    ((Soundset) CBsoundsets.getSelectedItem()).addSound(Soundset.ATTACK, (Sound) Lsounds.getSelectedValue());
                } else if (Rbirth.isSelected()) {
                    ((Soundset) CBsoundsets.getSelectedItem()).addSound(Soundset.BIRTH, (Sound) Lsounds.getSelectedValue());
                } else if (Rconfirm.isSelected()) {
                    ((Soundset) CBsoundsets.getSelectedItem()).addSound(Soundset.CONFIRM, (Sound) Lsounds.getSelectedValue());
                } else if (Rdeath.isSelected()) {
                    ((Soundset) CBsoundsets.getSelectedItem()).addSound(Soundset.DEATH, (Sound) Lsounds.getSelectedValue());
                } else if (Rpissed.isSelected()) {
                    ((Soundset) CBsoundsets.getSelectedItem()).addSound(Soundset.PISSED, (Sound) Lsounds.getSelectedValue());
                } else if (Rwhat.isSelected()) {
                    ((Soundset) CBsoundsets.getSelectedItem()).addSound(Soundset.WHAT, (Sound) Lsounds.getSelectedValue());
                }
                Lsoundsetlist.updateUI();
            }
        }
    }//GEN-LAST:event_add1ActionPerformed

    private void LsoundsetlistValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_LsoundsetlistValueChanged
        Sound s = (Sound) Lsoundsetlist.getSelectedValue();
        buttonGroup1.clearSelection();
        switch (((Soundset) CBsoundsets.getSelectedItem()).getUsageforSound(s)) {
            case Soundset.ATTACK: {
                Rattack.setSelected(true);
                break;
            }
            case Soundset.BIRTH: {
                Rbirth.setSelected(true);
                break;
            }
            case Soundset.CONFIRM: {
                Rconfirm.setSelected(true);
                break;
            }
            case Soundset.DEATH: {
                Rdeath.setSelected(true);
                break;
            }
            case Soundset.PISSED: {
                Rwhat.setSelected(true);
                break;
            }
            case Soundset.WHAT: {
                Rwhat.setSelected(true);
                break;
            }
        }
    }//GEN-LAST:event_LsoundsetlistValueChanged

    private void RwhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RwhatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_RwhatActionPerformed

    private void RpissedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RpissedActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_RpissedActionPerformed

private void BremovesoundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BremovesoundActionPerformed
    if (Lsounds.getSelectedValue() != null) {
        om.removeFromAssets(ObjectManager.Dateityp.SOUND, (RTSObject) Lsounds.getSelectedValue());
        Lsounds.updateUI();
    }
}//GEN-LAST:event_BremovesoundActionPerformed

private void BremovesoundsetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BremovesoundsetActionPerformed
    om.removeFromAssets(ObjectManager.Dateityp.SOUNDSET, (RTSObject) CBsoundsets.getSelectedItem());
}//GEN-LAST:event_BremovesoundsetActionPerformed

private void playActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playActionPerformed
    if (Lsounds.getSelectedValue() != null) {
        ((Sound) Lsounds.getSelectedValue()).playSound();
    }
}//GEN-LAST:event_playActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Bcreatesoundset;
    private javax.swing.JButton Bimportsound;
    private javax.swing.JButton Bremovesound;
    private javax.swing.JButton Bremovesoundset;
    private javax.swing.JComboBox CBsoundsets;
    private javax.swing.JList Lsounds;
    private javax.swing.JList Lsoundsetlist;
    private javax.swing.JRadioButton Rattack;
    private javax.swing.JRadioButton Rbirth;
    private javax.swing.JRadioButton Rconfirm;
    private javax.swing.JRadioButton Rdeath;
    private javax.swing.JRadioButton Rpissed;
    private javax.swing.JRadioButton Rwhat;
    private javax.swing.JButton add1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton play;
    private javax.swing.JButton remove;
    // End of variables declaration//GEN-END:variables
}
